﻿<%@ Page Language="C#" MasterPageFile="../Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<%@ Import Namespace="Microsoft.Web.Mvc"%>

<asp:Content ID="indexHead" ContentPlaceHolderID="head" runat="server">
    <title>Admin</title>
</asp:Content>

<asp:Content ID="css_js" ContentPlaceHolderID="js_css" runat="server">
	<link href="/admin/scripts/uploadify/uploadify.css" rel="stylesheet" type="text/css" />
	<script type="text/javascript" src="/admin/scripts/uploadify/jquery-1.4.2.min.js"></script>
	<script type="text/javascript" src="/admin/scripts/uploadify/swfobject.js"></script>
	<script type="text/javascript" src="/admin/scripts/uploadify/jquery.uploadify.v2.1.4.min.js"></script> 
	<style>
	#imgUploader {
		  position:relative;
		  left: -102px;
		}
	</style>
</asp:Content>

<asp:Content ID="head_content" ContentPlaceHolderID="head_content" runat="server">
    <% Html.RenderAction("index","head"); %>
</asp:Content>

<asp:Content ID="indexContentRight" ContentPlaceHolderID="main_content" runat="server">
  <div class="breadcrumb">
    <a href="/admin/home">Trang Chủ Admin</a>:: Chi Tiết Sách
</div>
  <div class="warning" style="display:none;"></div>
<div class="box">
  <div class="left"></div>
  <div class="right"></div>
  <div class="heading">
    <h1 style="background-image: url('image/product.png');"><%if(ViewData["action"].ToString()=="new") { %>Thêm Mới<%}else { %>Cập Nhật<%} %> Sách</h1>
    <div class="buttons"><a onclick="check_submit();" class="button"><span><%if(ViewData["action"].ToString()=="new") { %>Thêm<%}else { %>Cập Nhật<%} %></span></a><a onclick="location = '/admin/book';" class="button"><span>Huỷ bỏ</span></a></div>
  </div>
  <div class="content">
    <form action="/admin/book/<%=ViewData["action"] %>Book" method="post" enctype="multipart/form-data" id="form">
      <input type="hidden" id="book_id" name="book_id" value="<%=ViewData["book_id"] %>" />
      <table class="form">
        <tbody><tr>
          <td><span class="required">*</span> Mã sách:</td>
          <td><input maxlength="10" id="book_code" name="book_code" type="text" value="<%=ViewData["book_code"] %>" />
            </td>
        </tr>
        <tr>
          <td><span class="required">*</span> Tên sách:</td>
          <td><input maxlength="100" id="book_name" name="book_name" type="text" value="<%=ViewData["book_name"] %>" /></td>
        </tr>		
        <tr>
          <td><span class="required">*</span>Tác giả:</td>
          <td><input maxlength="100" id="author_name" name="author_name" type="text" value="<%=ViewData["author_name"] %>" /> </td>
        </tr>
        <tr>
          <td>Thể loại:</td>
          <td>
			<select name="category_id">
               <% string[][] category = (string[][])ViewData["allCate"]; %>
	            <% if (category != null && category.Length > 0)  {%>
                  <% foreach (string[] row in category)  { %>
                       <% if (row != null) {%>  
                         <option <% if(Convert.ToInt32(ViewData["category_id"])== Convert.ToInt32(row[0]) ){ %> selected="selected"<% } %> value="<%= row[0] %>"><%= row[1] %></option>     
                       <%}  }  } %>
            </select>
           </td>
        </tr>
        <tr>
          <td><span class="required">*</span>Giá bán:</td>
          <td><input id="price_buy" maxlength="10" onkeypress='validate(event)' name="price_buy" type="text" value="<%=ViewData["price_buy"] %>" /></td>
        </tr>		
        <tr>
          <td><span class="required">*</span>Số lượng bán:</td>
          <td><input id="quantity" maxlength="3" onkeypress='validate(event)' name="quantity" type="text" value="<%=ViewData["quantity"] %>" /></td>
        </tr>
        <tr>
          <td><span class="required">*</span>Giá thuê:</td>
          <td><input id="price_rent" maxlength="10" onkeypress='validate(event)' name="price_rent" type="text" value="<%=ViewData["price_rent"] %>" />
            </td>
        </tr>		
        <tr>
          <td><span class="required">*</span>Số lượng thuê:</td>
          <td><input id="quantity_rent" maxlength="3" onkeypress='validate(event)' name="quantity_rent" type="text" value="<%=ViewData["quantity_rent"] %>" />
            </td>
        </tr>
		<tr>
			<td>
			Hình ảnh:
			<br>
			<span class="help">Bấm vào hình để thay đổi.</span>
			</td>
			<td>
				<img id="preview" onerror=this.src='/content/images/no_image.jpg' class="image" src="<%=ViewData["image_url"] %>" width="100" height="100" />
				<input id="image_url" type="hidden" name="image_url" value="<%=ViewData["image_url_hidden"] %>" />
				<input type="hidden" name="old_image_url" value="<%=ViewData["image_url_hidden"] %>" />
			    <input type="file" id="img" name="img" />
			</td>
		</tr>		
        <tr>
          <td>Mô tả ngắn:</td>
          <td>
			<textarea rows="4" cols="60" name="short_description"><%=ViewData["short_description"]%></textarea>
          </td>
        </tr>
        <tr>
          <td>Mô tả chi tiết:</td>
          <td>
			<textarea name="long_description" id="ckdesc"><%=ViewData["long_description"]%></textarea>
          </td>
        </tr>		
        <tr>
          <td>Trạng thái:</td>
          <td>
               <select name="status">
                <option value="0">Disabled</option>
                <option value="1" <% if(Convert.ToInt32(ViewData["status"])== 1){ %> selected="selected"<% } %>>Enabled</option>
               </select>
          </td>
        </tr>
      </tbody></table>
    </form>
  </div>
</div>
</div>
<script type="text/javascript" src="/admin/scripts/ckeditor/ckeditor.js"></script>
<script type="text/javascript"><!--
    CKEDITOR.replace('ckdesc', {
        filebrowserBrowseUrl: '#',
        filebrowserImageBrowseUrl: '#',
        filebrowserFlashBrowseUrl: '#',
        filebrowserUploadUrl: '#',
        filebrowserImageUploadUrl: '#',
        filebrowserFlashUploadUrl: '#'
    });
//--></script>

<script type="text/javascript"><!--
    $("#img").uploadify({
        'uploader': '/admin/scripts/uploadify/uploadify.swf',
        'script': '/admin/scripts/uploadify/Upload.ashx',
        'cancelImg': '/admin/scripts/uploadify/cancel.png',
        'folder': '/Data/Temp',
        'multi': false,
        'auto': true,
        'fileExt': '*.jpg;*.gif;*.png;*.bmp',
        'fileDesc': 'Image Files (.JPG, .GIF, .PNG, .BMP)',
        'removeCompleted': true,
        'wmode': 'transparent',
        'hideButton': true,
        'width': 100,
        'height': 100,
        'displayData': 'percentage',
        'onComplete': function(event, ID, fileObj, response, data) {
            if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            }
            else {// code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }

            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    if (xmlhttp.responseText == -3)
                        document.location.href = "/admin/login";
                    else if (xmlhttp.responseText == -1) {
                        $(".warning").html("Upload file ảnh không thành công !");
                        $(".warning").show();
                    }
                    else {
                        $("#preview").attr("src", '/Data/Temp/' + xmlhttp.responseText);
                        $("#image_url").val(xmlhttp.responseText);
                        $(".warning").hide();
                    }
                }
            }

            xmlhttp.open("GET", "/admin/book/jsonRenameFile?name=" + fileObj.name, true);
            xmlhttp.send();
        }
    });
//--></script>

<script type="text/javascript">
    function check_submit() {
        if ($.trim($('#book_code').val()) == "") {
            $(".warning").html("Bạn chưa nhập mã sách !");
            $(".warning").show();
        }
        else if ($.trim($('#book_name').val()) == "") {
            $(".warning").html("Bạn chưa nhập tên sách !");
            $(".warning").show();
        }
        else if ($.trim($('#author_name').val()) == "") {
            $(".warning").html("Bạn chưa nhập tên tác giả !");
            $(".warning").show();
        }
        else if ($.trim($('#price_buy').val()) == "") {
            $(".warning").html("Bạn chưa nhập giá bán !");
            $(".warning").show();
        }
        else if ($.trim($('#quantity').val()) == "") {
            $(".warning").html("Bạn chưa nhập số lượng bán !");
            $(".warning").show();
        }
        else if ($.trim($('#price_rent').val()) == "") {
            $(".warning").html("Bạn chưa nhập giá thuê !");
            $(".warning").show();
        }
        else if ($.trim($('#quantity_rent').val()) == "") {
            $(".warning").html("Bạn chưa nhập số lượng thuê !");
            $(".warning").show();
        }
        else {
            if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            }
            else {// code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }

            xmlhttp.onreadystatechange = function() {
                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    if (xmlhttp.responseText == -3)
                        document.location.href = "/admin/login";
                    else if (xmlhttp.responseText != 0) {
                        $(".warning").html("Mã sách này đã được sử dụng bởi 1 sách khác !");
                        $(".warning").show();
                    }
                    else $('#form').submit();
                }
            }
            if ($.trim($('#book_id').val()) != "")
                xmlhttp.open("GET", "/admin/book/jsonCheckCode?id=" + $.trim($('#book_id').val()) + "&code=" + $.trim($('#book_code').val()), true);
            else 
                xmlhttp.open("GET", "/admin/book/jsonCheckCode?id=-1&code=" + $.trim($('#book_code').val()), true);
           
            xmlhttp.send();
        }
    }

    function validate(evt) {
        var theEvent = evt || window.event;
        var key = theEvent.keyCode || theEvent.which;
        if (key != 8) {
            key = String.fromCharCode(key); ;
            var regex = /[0-9]/;
            if (!regex.test(key)) {
                theEvent.returnValue = true;
                if (theEvent.preventDefault) theEvent.preventDefault();
            }
        }
    }
</script>
</asp:Content>
